package event

import (
	"os"
	"sjy/customcmd/src/constants"
	"sjy/customcmd/src/entity"

	"github.com/ahmetb/go-linq/v3"
)

func Parsing() *entity.Param {
	// 获取参数
	args := os.Args[1:]
	res := &entity.Param{
		OrgArgs: args,
	}

	// 空参数的情况
	allEmpty := linq.From(args).All(func(item interface{}) bool { return item == "" })
	if len(args) == 0 || allEmpty {
		res.MainCmd = constants.MasterCmd
		return res
	}

	res.MainCmd = findMainCmd(args[0])
	res.CmdArgs = args[1:]

	return res
}

func findMainCmd(cmd string) *constants.Cmd {
	var result *constants.Cmd
	found := linq.From(constants.MainCmds).WhereT(func(cobj *constants.Cmd) bool {
		return cobj.Cmds.Contains(cmd)
	}).First()
	if found != nil {
		result = found.(*constants.Cmd)
		return result
	}
	return constants.CustomCmd
}
